/*!
 * Helper 
 *
 * Date: 2016-5-24
 */
(function( window ){
'use strict';

function Notification(){
    return new Notification.fn.init();
}

Notification.fn = Notification.prototype = {

    init:function(){

        var HelperNotification = '<div id="HelperNotification" style="width: 250px;height: auto; position: fixed; bottom: 10px; left: 5px; z-index:999999; "></div>';

        $('body').append(HelperNotification);
    },

    notification:function(type, text, delay, callback){
        if(arguments.length < 3 || arguments.length === 3 && typeof(arguments[2])==='function'){return false;}

        if(!$('#HelperNotification'))
        {
            var HelperNotification = '<div id="HelperNotification" style="width: 250px;height: auto; position: fixed; bottom: 10px; left: 5px; "></div>';
            $('body').append(HelperNotification);
        }

        var base_style_1 = 'left:-260px; width: 250px; height: auto; position: relative; margin-bottom:10px; overflow: hidden; border-radius: 5px; overflow:hidden;';
        var base_style_2 = 'width: 250px; height: auto; position: relative;padding:0;';
        var base_style_3 = 'width: 18px; height: 18px; border-radius: 10px; background: inherit;font-weight: bold; font-size: 16px; position: absolute;top: 5px; right: 5px; line-height: 20px; display: inline-block; text-align: center; cursor: pointer;';
        var base_style_4 = 'padding: 25px 15px;font-weight:bold; font-size: 14px; width:250px; display:inline-block; height:auto;word-wrap:break-word;';

        var style1, style2, style3, style4;
        switch (type)
        {
            case 'success':
                style1 = 'border:#d6e9c6 1px solid; box-shadow: 0 3px 10px -3px #356635; ';
                style2 = 'background: #d6e9c6;';
                style3 = 'color: #356635;';
                style4 = 'color: #356635;';
                break;
            case 'info':
                style1 = 'border:#bce8f1 1px solid; box-shadow: 0 3px 10px -3px #2d6987; ';
                style2 = 'background: #d9edf7;';
                style3 = 'color: #2d6987;';
                style4 = 'color: #2d6987;';
                break;
            case 'warning':
                style1 = 'border:#faebcc 1px solid; box-shadow: 0 3px 10px -3px #a47e3c; ';
                style2 = 'background: #fcf8e3; ';
                style3 = 'color: #a47e3c;';
                style4 = 'color: #a47e3c;';
                break;
            case 'error':
                style1 = 'border:#ebccd1 1px solid; box-shadow: 0 3px 10px -3px #953b39;';
                style2 = 'background: #f2dede; ';
                style3 = 'color: #b94a48;';
                style4 = 'color: #953b39;';
                break;
        }

        var timestamp = new Date().getTime();
        timestamp += this.random(4);
        var html =  '<div class="HelperNotification" id="HelperNotification'+timestamp+'" style="'+base_style_1+style1+'">'+
                    '<div class="container" style="'+base_style_2+style2+'">'+
                    '<div class="close" style="'+base_style_3+style3+'">×</div>'+
                    '<div style="'+base_style_4+style4+'">'+text+'</div></div></div>';

        //添加到DOM
        $('#HelperNotification').append(html);
        //进场动画
        $('#HelperNotification'+timestamp).animate({

            left:0

        },300);

        $('.HelperNotification .container .close').on('click',function(){
            $(this).parents('.HelperNotification').animate({
                height:0
            },200,function(){$(this).remove();});
            // $(this).parents('.HelperNotification').fadeOut('400', function() {
            //     $(this).remove();
            // });
        });

        var t = setTimeout(function(){
            // $('#HelperNotification'+timestamp).fadeOut('400', function() {
            //     $(this).remove();
            // });
            $('#HelperNotification'+timestamp).animate({
                left:'-260px',
                opacity:0
            },200,function(){
                $(this).animate({
                    height:0
                },100,function(){
                    $(this).remove();
                });
            });
        },delay);

        if(callback && typeof callback === 'function'){
            callback();
        }
    },

    random:function(num){
        var char = ['0','1','2','3','4','5','6','7','8','9',
        'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
        'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
        var randomNum = '';
        for(var i=0;i<num;i++)
        {
            randomNum += char[parseInt(Math.random()*(char.length+1))];
        }
        return randomNum;
    }
};

Notification.fn.init.prototype = Notification.fn;

var N = new Notification();

window.N = N;

window.Notification = N;

})( window );